package com.bsoft.gol.editor.service.impl;


import com.bsoft.gol.editor.dao.EditorDictDAO;
import com.bsoft.gol.editor.editor.EditorDict;
import com.bsoft.gol.editor.editor.qo.EditorDictQo;
import com.bsoft.gol.editor.editor.response.ExamItemResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bsoft.gol.editor.service.EditorDictApi;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 字典表
 *
 * @author sumu
 */
@Service("editorDictApi")
public class EditorDictApiImpl implements EditorDictApi {
    private static Logger logger = LoggerFactory.getLogger(EditorDictApiImpl.class);

    @Autowired
    private EditorDictDAO editorDictDAO;

    @Override
    public List<EditorDict> list(EditorDictQo editorDictQo) {
        return editorDictDAO.queryEditorDictInfoList(editorDictQo);
    }

    @Override
    public List<EditorDict> codelist(EditorDictQo editorDictQo) {
        return editorDictDAO.queryEditorDictInfoListBycode(editorDictQo);
    }

    @Override
    public String addOrUpdate(EditorDictQo qo) {

        if (CommonUtils.isNotEmpty(qo.getDictId())) {
            // 编辑
            EditorDict editorDict = editorDictDAO.get(qo.getDictId());

            if (CommonUtils.isNotEmpty(editorDict)) {
                if (CommonUtils.isNotEmpty(qo.getDictName())) {
                    editorDict.setDictName(qo.getDictName());
                }

                if (CommonUtils.isNotEmpty(qo.getDictState())) {
                    editorDict.setDictState(qo.getDictState());
                }

                if (CommonUtils.isNotEmpty(qo.getParentId())) {
                    editorDict.setParentId(qo.getParentId());
                }

                if (CommonUtils.isNotEmpty(qo.getDictCode())) {
                    editorDict.setDictCode(qo.getDictCode());
                }

                if (CommonUtils.isNotEmpty(qo.getDictLeafNodeFlag())) {
                    editorDict.setDictLeafNodeFlag(qo.getDictLeafNodeFlag());
                }

                if (CommonUtils.isNotEmpty(qo.getDictAddress())) {
                    editorDict.setDictAddress(qo.getDictAddress());
                }

                if (CommonUtils.isNotEmpty(qo.getDictParam())) {
                    editorDict.setDictParam(qo.getDictParam());
                }

                if (CommonUtils.isNotEmpty(qo.getDictType())) {
                    editorDict.setDictType(qo.getDictType());
                }

                editorDictDAO.update(editorDict);
            }

        } else {
            // 新增
            EditorDict editorDict = new EditorDict();
            BeanUtils.copyProperties(qo, editorDict);

            editorDict.setDictState((byte) 2);
            editorDict.setCreateTime(new Date());
            String uuid = UUID.randomUUID().toString();
            editorDict.setDictId(uuid);
            editorDictDAO.save(editorDict);
            return uuid;
        }
        return null;
    }

    @Override
    public void delete(String dictId) {
        editorDictDAO.remove(dictId);
    }

    @Override
    public void changeState(String dictId, Byte dictState) {

        editorDictDAO.updateEditorDictState(dictId,dictState);
    }

    @Override
    public void saveExamItem(List<ExamItemResponse> items) {

        if (CommonUtils.isNotEmpty(items)) {
            List<EditorDict> list = new ArrayList<>();
            for (ExamItemResponse item : items) {
                EditorDict editorDict = new EditorDict();
                editorDict.setDictName(item.getExamName());
                editorDict.setDictCode(item.getExamCode());
                editorDict.setParentId(item.getExamParentCode());
                editorDict.setDictState((byte) 2);
                editorDict.setCreateTime(new Date());
                editorDict.setDictType((byte) 3);
                String uuid = UUID.randomUUID().toString();
                editorDict.setDictId(uuid);
                list.add(editorDict);
            }

            for (EditorDict dict : list) {
                if (CommonUtils.isNotEmpty(dict.getParentId())) {
                    for (EditorDict item : list) {
                        if (dict.getParentId().equals(item.getDictCode())) {
                            dict.setParentId(item.getDictId());
                            break;
                        }
                    }
                }else {
                    dict.setParentId("0");
                }
                editorDictDAO.save(dict);
            }

//            editorDictManager.insertBatch(list);
        }
    }

    @Override
    public List<EditorDict> detail(String dictId) {

        return editorDictDAO.queryEditorDictByParentId(dictId);
    }

    @Override
    public EditorDict oneDetail(String dictId) {

        return editorDictDAO.get(dictId);
    }
}